<?php
include_once 'DataForm/DataForm.php';
include_once 'DataGrid/Form/FormSearch.php';
include_once 'DataGrid/DataGrid.php';

class Admin_Model_Details extends NadebZend_Model_Crud
{
	public function __construct()
	{
    	$this->tb = new Application_Model_Db_Details();
    	
    	parent::__construct();
	}
	
	public function data_grid()
	{
		$modelPath = __LINKS__ . 'admin/' . $this->_params->controller;
		$data = $this->get_all('order ASC','idProduct = '.$this->_params->rel);
		
		$grid = new DataGrid( 'nadeb-grid' );
		$grid->title( 'Detalhes do Produto' );
		$grid->primary( $this->primary );
		$grid->setData( $data );
		$grid->setAction( $modelPath.'/delete/' );
		$grid->setControllers( new DeleteButton('delete_button', 'Excluir') );
		$grid->setControllers( new Link('insert_button', 'Inserir', $modelPath . '/edit/rel/' . $this->_params->rel) );
		$grid->setTools( new LinkTool('Editar Estoque', __LINKS__ . 'admin/stock/list/sub/' . $this->_params->rel . '/relsub', 'edit_rel') );
		$grid->setTools( new MoveTool('Mover', $modelPath . '/save-order') );
		$grid->setTools( new EditTool('Editar', $modelPath . '/edit/rel/' . $this->_params->rel) );
		$grid->setTools( new DeleteTool('Excluir', $modelPath . '/delete') );
		$grid->setHeader( array( 'Código' => 'tm50', 'Nome', 'Cor' => 'tm100', 'Preço' => 'tm50' ) );
		$grid->setRows( array(
						'code', 
						'idProduct' => new GArray( $this->productOptions() ), 
						'idColor' => new GArray( $this->colorOptions() ), 
						'cost' ));
		return $grid->get();
	}
	
	public function data_form($id = null, $post = null)
	{
        $header      = HeaderController::get_instance();
		$header->js  = "public/admin/js/details.js";
		$header->js  = "public/admin/js/jquery.price_format.1.5.js";
        
		$data = $id ? $this->get_one($id) : null;
		$picFolder =  __ROOT__ . 'public/uploads/';
		$picRemove = $data ? $this->getClearPictureUrl($data['image'], $data['idDetail'], 'image') : null;
		
		$form = new DataForm( 'nadeb-form' );
		$form->setData( $data );
		$form->title( 'Cadastro de Detalhes do Produto' );
		$form->form()->action = __LINKS__ . 'admin/'. $this->_params->controller .'/edit/' . ( ($id) ? 'id/' . $id : '' );
		$form->add( new InputHidden('idProduct', $this->_params->rel) );
		$form->add( new Select('idColor','Cor',$this->colorOptions()) );
		$form->add( new InputText('cost', 'Preço') );
		$form->add( new InputText('code','Código') );
		$form->add( new InputFile('image','Imagem (190 x 350)', null, null, $picFolder, $picRemove) );
		$form->fieldset('buttons');
		$form->add( new InputSubmit('btn_save', 'salvar') );
		$form->add( new InputButton('btn_cancel', 'cancelar') );
		$form->jsFolder( new JSFolder('folder', 'Galeria de imagens(1080x1178)') );
		
		if( $post )
		{
			if( !$_FILES['image']['name'] ) unset( $_FILES['image'] );
			$result = $this->save($id, $post, $form, $picFolder);
			return $result['message'];
		}
		else
		{
			return $form->get();
		}
	}
	
	function productOptions()
	{
		$tb = new Application_Model_Db_Products();
		$data = $tb->fetchAll('idProduct = ' . $this->_params->rel);
		foreach ($data as $value) $result[$value->idProduct] = $value->name;
		
		return $result;
	}
	
	function colorOptions()
	{
		$tb = new Application_Model_Db_Colors();
		$data = $tb->fetchAll('active = 1', 'order ASC');
		foreach ($data as $value) $result[$value->idColor] = $value->name;
		
		return $result;
	}
	
	function sizeOptions()
	{
		$tb = new Application_Model_Db_Sizes();
		$data = $tb->fetchAll('active = 1', 'order ASC');
		foreach ($data as $value) $result[$value->idSize] = $value->name;
		
		return $result;
	}
	
	public function typeOptions()
	{
		return array('1' => 'Sim', '0' => 'Não');
	}
	
	public function breadCrumbs($action)
	{
		$bc = array(
				'list' => array(
					'' => 'home',
					'products/list' => 'Listagem de Produtos',
					'#' => 'Detalhes do Produto'
			),
				'edit' => array(
					'' => 'home',
					'products/list' => 'Listagem de Produtos',
					'details/list/rel/' . $this->_params->rel => 'Detalhes do Produto',
					'#' => 'Cadastro de Detalhes do Produto'
			)
		);
	
		return $bc[$action];
	}
	
}


